<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Orders;


class OrderController extends Controller
{
    /**
     * 订单列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //分页,倒序,搜索
        $keywords = $request->input('keywords');
        //如果参数为空
        // if(empty($keywords)){
        //     $orders = DB::table('orders')
        //     ->orderBy('id','desc')
        //     ->paginate($request->input('num',10));
        // }else{
        //     $orders = DB::table('orders')
        //     ->orderBy('id','desc')
        //     ->where('onum','like','%'.$keywords.'%')
        //     ->paginate($request->input('num',10));
        // }


        $orders = Orders::orderBy('id','desc')
            ->where(function($query)use($keywords){
                if(!empty($keywords)){
                    $query->where('onum','like','%'.$keywords.'%');
                }
            })
            ->paginate($request->input('num',5));


        return view('webadmin.order.list',['orders'=>$orders,'data'=>$request->all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //获取当前用户所有的地址
        

        //获取数据
        $data = $request->only(['goods']);
        $goods = $data['goods'];
        //创建数据
        $res = [];
        $res['onum'] = $this->createNewOrderNumber();
        $res['uid'] = session('uid');
        $res['sendTime'] = time();
        $res['status'] = 0;
        //插入数据库
        $gid = DB::table('orders')->insertGetId($res);
        //插入附加表
        $data = [];
        foreach($goods as $k=>$v) {
            $temp = [];
            $temp['order_id'] = $gid;
            $temp['goods_id'] = $v['id'];
            $temp['num'] = $v['num'];
            $data[] = $temp;
        }
        $r = DB::table('order_goods')->insert($data);
        if($r) {
            return view('home.order.show', [
                'addresses' => $addresses
                ]);
        }else {
            echo 'fail';
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 取消订单
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $order = Orders::findOrfail($id);
        if($order->delete()){
            return redirect('/admin/order')->with('info','取消订单');
        }else{
            return back()->with('info','订单取消失败');
        }
    }

}
